package api

import (
	"ginfast/app/system/dto"
	"ginfast/app/system/model"
	"ginfast/pkg/ginx"
	"ginfast/pkg/orm"
	"ginfast/util"
)

// Notice 通知
type Notice struct {
}

// ListHandler 列表
func (Notice) ListHandler(c *ginx.Context) {
	var p dto.Where
	if err := c.MustBind(&p); err != nil {
		return
	}
	var data []model.Notice
	total, _ := p.DbModelWhere(&model.Notice{}).Find(&data)
	c.JSONWriteTotal(total, data)
}

// GetHandler 获取指定id
func (Notice) GetHandler(c *ginx.Context) {
	util.QueryByID(&model.Notice{}, c)
}

// AddHandler 新增
func (Notice) AddHandler(c *ginx.Context) {
	var p model.Notice
	//获取参数
	err := c.MustBind(&p.NoticeOpt)
	if err != nil {
		return
	}
	err = orm.DbCreate(&p)
	c.JSONWriteError(err)
}

// UpdateHandler 修改
func (Notice) UpdateHandler(c *ginx.Context) {
	var p model.Notice
	//获取参数
	err := c.MustBind(&p.NoticeOpt)
	if err != nil {
		return
	}
	err = orm.DbUpdateModel(&p)
	c.JSONWriteError(err)
}

// DeleteHandler 删除
func (Notice) DeleteHandler(c *ginx.Context) {
	util.Deletes(&model.Notice{}, c)
}

func (o Notice) Routers(r *ginx.RouterGroup) {
	r.GET("/list", o.ListHandler)
	r.GET("/:id", o.GetHandler)
	r.POST("", o.AddHandler)
	r.PUT("", o.UpdateHandler)
	r.DELETE("/:id", o.DeleteHandler)
}
